package com.example.service;

import com.example.mapper.CityMapper;
import com.example.pojo.*;
import com.example.pojo.mp.ProvinceData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CityService {
    @Autowired
    private CityMapper cityMapper;
    public List<CityVo> getCityList() {
       List<CityVo> list = cityMapper.getCityList();
//System.err.println(list);
       if (list!=null && list.size()>0){
           for (CityVo cityVo: list) {

                China china =cityMapper.getChinaById(cityVo.getCity_number());
                China parent = cityMapper.getChinaById(china.getParent_id());
                cityVo.setName(china.getName());
                cityVo.setProvince(parent.getName());

           }
       }

       return list;
    }

    public List<CityOption> getCityOption() {

        List<CityOption> list=cityMapper.getProvinceList();
        if (list!=null &&list.size()>0){
            for(CityOption cityOption:list){
                //通过省份的id查询对应的城市列表
                List<CityOption> children=cityMapper.getChildrenByParentId(cityOption.getValue());
                if(children!=null && children.size()>0){
                    cityOption.setChildren(children);
                    for (CityOption cityOption1:children){
                        List<CityOption> children1=cityMapper.getChildrenByParentId(cityOption1.getValue());
                        if(children1!=null && children1.size()>0) {
                            cityOption1.setChildren(children1);
                        }
                    }
                }
            }
        }
        return list;
    }

    public List<CityVo> getCityList(ConditionsQuery conditionsQuery) {
        List<CityVo> list = cityMapper.getCityList();
        List<CityVo> resultList=new ArrayList<>();
/*
    System.err.println(list);
    System.err.println(conditionsQuery.getConditions().get("cityName"));
*/
        if (list!=null && list.size()>0){
            for (CityVo cityVo: list) {

                China china =cityMapper.getChinaById(cityVo.getCity_number());
                China parent = cityMapper.getChinaById(china.getParent_id());
                cityVo.setName(china.getName());
                cityVo.setProvince(parent.getName());
                if(conditionsQuery.getConditions().get("cityName")!=null){
                    String cityName=conditionsQuery.getConditions().get("cityName");
                    if(cityVo.getName().contains(cityName) || cityVo.getProvince().contains(cityName)){
                        resultList.add(cityVo);
                    }
                }
            }
        }
        if(conditionsQuery.getConditions().get("cityName")!=null){
            return resultList;
        }
        return list;
    }

    public List<Region> getRegionList(ConditionsQuery conditionsQuery) {
        List<Region> list=cityMapper.getRegionList(conditionsQuery);
        return list;
    }

    public Result addRegion(Region region) {
        /**
         * 非空校验
         */
        if (region.getId() == null) {
            return new Result(100, "没有找到id", null);
        }
        if (!StringUtils.hasText(region.getName()) || region.getName()==null ) {
            return new Result(100, "区域编码不能为空", null);
        }
        if (region.getParentId() == null) {
            return new Result(100, "上级标识不能为空", null);
        }
        if (region.getLevel() == null) {
            return new Result(100, "区域等级不能为空", null);
        }
        if (!StringUtils.hasText(region.getCityCode()) || region.getCityCode()==null) {
            return new Result(100, "城市编码不能为空", null);
        }
        if (!StringUtils.hasText(region.getZipCode()) || region.getZipCode()==null) {
            return new Result(100, "邮政编码不能为空", null);
        }
        if (!StringUtils.hasText(region.getMerName()) || region.getMerName()==null) {
            return new Result(100, "组合名称不能为空", null);
        }
        if (region.getLng() == null) {
            return new Result(100, "经度不能为空", null);
        }
        if (region.getLat() == null) {
            return new Result(100, "纬度不能为空", null);
        }
        if (!StringUtils.hasText(region.getPinYin()) || region.getPinYin()==null) {
            return new Result(100, "拼音不能为空", null);
        }
        int row=cityMapper.addRegion(region);
        if(row!=1){
            return new Result(100, "操作失败", null);
        }
        return new Result(200, "添加成功", null);
    }

    public Result delRegionById(int id) {
        /**
         * 查询是否存在
         */
        Region r=cityMapper.getRegionById(id);
        if(r==null){
            return new Result(100,"id不存在",null);
        }
        int row=cityMapper.delRegionById(id);
        if (row!=1){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"操作成功",null);
    }

    public Result delRegionBatch(String ids) {
        if (!StringUtils.hasText(ids)){
            return new Result(100,"请至少选择一条记录",null);
        }

        String[] idStr =ids.split(",");

        List<Integer> idList=new ArrayList<>();

        for (int i=0; i<idStr.length;i++){
            idList.add(Integer.parseInt(idStr[i]));
        }
        int rows =cityMapper.delRegionBatch(idList);
        if(rows!=idList.size()){
            return new Result(100,"操作失败",null);
        }
        return new Result(200,"删除成功",null);
    }

    public Result getProvinceDetails(Integer id) {
        List<China> china=cityMapper.getProvinceDetails(id);
        List<ProvinceData> pd=new ArrayList<>();
        for (China c:china){
            ProvinceData p=new ProvinceData();
            p.setCode(c.getId());
            p.setName(c.getName());
            pd.add(p);
        }
        System.err.println(pd);
        return new Result(200,"成功",pd);
    }
}
